home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / delicious_bookmarks-2.0.64-fx.xpi / chrome / deliciousBookmarks.jar / content / yDisableExtensions.js < prev    next >
Encoding:
Text File  |  2008-06-19  |  5.6 KB  |  166 lines

  1. /* YBookmarks extension ID. */
  2. const YBOOKMARKS_ID = "{2fa4ed95-0317-4c6a-a74c-5f3e3912c1f9}";
  3. /* IDs of extensions that are incompatible with YBookmarks. */
  4. const Y_BLOCK_LIST =
  5.    ["{5a2b4e34-ce62-42e9-a658-06ba4490adf8}"]; //classic del.icio.us
  6. const Y_ID_PREFIX = "urn:mozilla:item:";
  7. /* property used to flag disabled extensions. */
  8. const Y_DISABLED_PROPERTY = "http://www.mozilla.org/2004/em-rdf#userDisabled";
  9.  
  10. /**
  11.  * Handles showing a list of all incompatible extensions to the user, and
  12.  * disabling all of them, or alternatively disabling YBookmarks. This is meant
  13.  * to prevent problems such as having more than one extension writing to the
  14.  * bookmarks datasource.
  15.  */
  16. var yDisableExtensions = {
  17.    /* list of incompatible extensions. */
  18.    _blockList : null,
  19.    
  20.    /**
  21.     * Look for extensions that are known to be incompatible with YBookmarks. If
  22.     * any are found, show a dialog to prompt the user for action.
  23.     * @return an object with one attribute: foundList, holding the array of
  24.     * found incompatible extensions
  25.     */
  26.    getIncompatibleExtensions: function() {
  27.       var result = new Object();
  28.       var em = 
  29.          Components.classes["@mozilla.org/extensions/manager;1"].
  30.             getService(Components.interfaces.nsIExtensionManager);
  31.       var extensionList =
  32.          em.getItemList(Components.interfaces.nsIUpdateItem.TYPE_EXTENSION, {});
  33.       var foundList = new Array();
  34.       var extId;
  35.       
  36.       yDebug.print("Looking for incompatible extensions.", YB_LOG_MESSAGE);
  37.  
  38.       for (var i = 0; i < extensionList.length; i++) {
  39.          extId = extensionList[i].id;
  40.           
  41.          for (var j = 0; j < Y_BLOCK_LIST.length; j++) {
  42.             if (Y_BLOCK_LIST[j] == extId) {
  43.                if (this._isEnabled(extId, em)) {
  44.                   foundList.push(extensionList[i]);
  45.                }
  46.  
  47.                break;
  48.             }
  49.          }
  50.       }
  51.       
  52.       yDebug.print(
  53.          "Found " + foundList.length + " incompatible extensions.",
  54.          YB_LOG_MESSAGE);
  55.       
  56.       result.foundList = foundList;      
  57.       return result;
  58.    },
  59.  
  60.    /**
  61.     * Determine if an extension is enabled.
  62.     * @param extensionId the ID of the extension.
  63.     * @param em the extension manager component.
  64.     * @return true if the extension is enabled. false otherwise.
  65.     */
  66.    _isEnabled: function(extensionId, em) {
  67.       var result = true;
  68.       var rs =
  69.          Components.classes["@mozilla.org/rdf/rdf-service;1"].
  70.             getService(Components.interfaces.nsIRDFService);
  71.       var extensionDS = em.datasource;
  72.       var resourceId = rs.GetResource(Y_ID_PREFIX + extensionId);
  73.       var propertyDisabled = rs.GetResource(Y_DISABLED_PROPERTY);
  74.       var resourceDisabled =
  75.          extensionDS.GetTarget(resourceId, propertyDisabled, true);
  76.  
  77.       /* if the property is not set, the extension is enabled. */
  78.       if (resourceDisabled) {   
  79.          resourceDisabled.QueryInterface(Components.interfaces.nsIRDFLiteral);
  80.          result = (resourceDisabled.Value != "true");
  81.       }
  82.  
  83.       return result;
  84.    },
  85.    
  86.    /**
  87.     * Initializes the dialog by filling the incompatible extension list.
  88.     * @return returns true for success.
  89.     */
  90.    init : function() {
  91.       var extensionList = document.getElementById("extension-list");
  92.       var insertedItem;
  93.       
  94.       this._blockList = window.arguments[0];
  95.       
  96.       for (var i = 0; i < this._blockList.length; i++) {
  97.          insertedItem = extensionList.appendItem(
  98.             this._blockList[i].name, this._blockList[i].id);
  99.         insertedItem.setAttribute("class", "listitem-iconic");
  100.         insertedItem.setAttribute("image", this._blockList[i].iconURL);
  101.       }
  102.       
  103.       return true;
  104.    },
  105.  
  106.    /**
  107.     * Disables all incompatible extensions and prompts the user to restart.
  108.     * @return returns true for success.
  109.     */
  110.    disableExtensions : function() {
  111.       yDebug.print("Disabling incompatible extensions.", YB_LOG_MESSAGE);
  112.       var em = 
  113.          Components.classes["@mozilla.org/extensions/manager;1"].
  114.             getService(Components.interfaces.nsIExtensionManager);
  115.       var os =
  116.          Components.classes["@mozilla.org/observer-service;1"].
  117.             getService(Components.interfaces.nsIObserverService);         
  118.  
  119.       for (var i = 0; i < this._blockList.length; i++) {
  120.          em.disableItem(this._blockList[i].id);
  121.       }
  122.             
  123.       os.notifyObservers(null, "ybookmark.disableExtension", "accept");
  124.       
  125.       return true;
  126.    },
  127.    
  128.    /**
  129.     * Cancels the incompatible extension dialog. It notifies an observer to
  130.     * disable this extension.
  131.     * @return returns true for success.
  132.     */
  133.    cancelDialog : function() {
  134.      var os = Components.classes["@mozilla.org/observer-service;1"].
  135.                   getService(Components.interfaces.nsIObserverService);              
  136.      os.notifyObservers(null, "ybookmark.disableExtension", "cancel");
  137.  
  138.      return true;
  139.    },
  140.    
  141.    /**
  142.     * Disables the Ybookmarks extension and prompts the user to restart.
  143.     * @return returns true for success.
  144.     */
  145.    disableYBookmarks : function() {
  146.      var em = 
  147.         Components.classes["@mozilla.org/extensions/manager;1"].
  148.            getService(Components.interfaces.nsIExtensionManager);
  149.          
  150.       em.disableItem(YBOOKMARKS_ID);
  151.    },
  152.    
  153.     /**
  154.     * Uninstalls the Ybookmarks extension
  155.     */
  156.     uninstallYBookmarks : function() {
  157.         var em = 
  158.            Components.classes["@mozilla.org/extensions/manager;1"].
  159.               getService(Components.interfaces.nsIExtensionManager);
  160.  
  161.         em.uninstallItem(YBOOKMARKS_ID);
  162.     },
  163.  
  164. };
  165.  
  166.